//
//  UIView+NEDBadge.m
//  MoreCoin
//
//  Created by 林宁宁 on 2019/9/23.
//  Copyright © 2019 MoreCoin. All rights reserved.
//

#import "UIView+NEDBadge.h"
#import "NSMutableAttributedString+NED.h"
#import "NSString+NED.h"
#import "UIView+NED.h"
#import "UIColor+NEDStyle.h"
#import "UIFont+NED.h"
#import <objc/runtime.h>
#import <NEDConstant/NEDConstant.h>
#import <Masonry/Masonry.h>
#import "NSObject+NEDDealloc.h"
#import <NEDConstant/NEDConstant+Method.h>

@implementation UIView (NEDBadge)

-(NEDBadgeCountView *)badgeCountView{
    NEDBadgeCountView * badgeV = objc_getAssociatedObject(self, @selector(badgeCountView));
    if(!badgeV){
        badgeV = [NEDBadgeCountView new];
        badgeV.position = NEDBadgeCountPosition_RightTop;
        badgeV.layer.borderWidth = 0.5;
        badgeV.layer.borderColor = [UIColor ned_white].CGColor;
        badgeV.badgeHeight = 14;
        [self addSubview:badgeV];
        self.badgeCountView = badgeV;
        
//        self.backgroundColor = UIColor.ned_randomColor;
    }
    return badgeV;
}
-(void)setBadgeCountView:(NEDBadgeCountView *)badgeCountView{
    objc_setAssociatedObject(self, @selector(badgeCountView), badgeCountView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end





@interface NEDBadgeCountView()

@property (retain, nonatomic) UIImageView * backgroundImageView;

@end

@implementation NEDBadgeCountView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.pointSize = 6;
        self.badgeHeight = 12;
        self.contentInset = UIEdgeInsetsMake(0, 3, 0, 3);
        
        self.cornerPosType = UIRectCornerAllCorners;
        self.badgeBackgroundColor = [UIColor ned_badge];
        
        [self setTitleColor:[UIColor ned_white] forState:(UIControlStateNormal)];
        self.titleLabel.font = [UIFont ned_pfOfSize:9];
        self.hidden = YES;
        
    }
    return self;
}

-(void)didMoveToSuperview{
    [super didMoveToSuperview];
    [self updateBadgeConstraints];
}

-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    
    if(self.cornerPosType != UIRectCornerAllCorners){
        self.layer.borderWidth = 0;
    }
    [self corner:self.badgeHeight*0.5 pos:self.cornerPosType];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.backgroundColor = self.badgeBackgroundColor;
    });
}

- (void)setcenterOffetView:(UIView *)centerOffetView{
    _centerOffetView = centerOffetView;
    if(!centerOffetView){
        return;
    }
    centerOffetView.clipsToBounds = NO;
    [self updateBadgeConstraints];
}

- (void)setPosition:(NEDBadgeCountPosition)pos{
    _position = pos;
    [self updateBadgeConstraints];
}

-(void)setCenterOffet:(CGPoint)centerOffet{
    _centerOffet = centerOffet;
    [self updateBadgeConstraints];
}

-(void)setContentInset:(UIEdgeInsets)contentInset{
    _contentInset = contentInset;
    [self updateBadgeConstraints];
}


-(void)setBadgeCount:(NSString *)badgeCount{
    _badgeCount = badgeCount;
    if(IsNull(badgeCount) && !self.isShowZero){
        self.hidden = YES;
    }else{
        self.hidden = NO;
        if([badgeCount ned_isPureInt]){
            if([badgeCount integerValue] == 0 && !self.isShowZero){
                self.hidden = YES;
                return;
            }
            if([badgeCount integerValue] > 99){
                badgeCount = @"99+";
            }
        }
        if(self.isShowZero && IsNull(badgeCount)){
            badgeCount = @"0";
        }
        [self setTitle:badgeCount forState:(UIControlStateNormal)];
        [self updateBadgeConstraints];
    }
}

-(void)setIsPointShow:(BOOL)isPointShow{
    _isPointShow = isPointShow;
    self.hidden = !isPointShow;
    self.cornerPosType = UIRectCornerAllCorners;
    [self setTitle:@"" forState:(UIControlStateNormal)];
    
    [self updateBadgeConstraints];
}

-(void)setBadgeHeight:(CGFloat)badgeHeight{
    _badgeHeight = badgeHeight;
    self.layer.cornerRadius = badgeHeight*0.5;
    [self updateBadgeConstraints];
}

- (void)updateBadgeConstraints{
    if(!self.superview){
        return;
    }
    
    self.translatesAutoresizingMaskIntoConstraints = NO;
    
    MASConstraintMaker * make = [[MASConstraintMaker alloc] initWithView:self];
    make.removeExisting = YES;
    
    CGFloat width = self.badgeHeight;
    CGFloat height = self.badgeHeight;
    
    if(!self.isPointShow){
        NSAttributedString * attStr = [self currentAttributedTitle];
        if(!attStr){
            attStr = [NSMutableAttributedString ned_initWithStr:[self currentTitle] customBlock:^(NSMutableAttributedString *att) {
                [att ned_font:self.titleLabel.font];
            }];
        }
        CGSize sizeStr = [attStr ned_getSizeConstrainedToSize:CGSizeMake(FLT_MAX, self.badgeHeight)];
        width = MAX(sizeStr.width + self.contentInset.left + self.contentInset.right, self.badgeHeight);
    }
    
    UIView * alignmentView = self;
    if(self.superview){
        alignmentView = self.superview;
    }
    if(self.centerOffetView){
        alignmentView = self.centerOffetView;
    }
    CGFloat offsetX = self.centerOffet.x ;
    CGFloat offsetY = self.centerOffet.y;
    switch (self.position) {
        case NEDBadgeCountPosition_RightTop:
            make.left.equalTo(alignmentView.mas_right).offset(offsetX);
            make.bottom.equalTo(alignmentView.mas_top).offset(offsetY);
            break;
        case NEDBadgeCountPosition_RightBottom:
            make.left.equalTo(alignmentView.mas_right).offset(offsetX);
            make.top.equalTo(alignmentView.mas_bottom).offset(offsetY);
            break;
        case NEDBadgeCountPosition_RightCenter:
            make.left.equalTo(alignmentView.mas_right).offset(offsetX);
            make.centerY.equalTo(alignmentView.mas_centerY).offset(offsetY);
            break;
        case NEDBadgeCountPosition_LeftTop:
            make.right.equalTo(alignmentView.mas_left).offset(offsetX);
            make.bottom.equalTo(alignmentView.mas_top).offset(offsetY);
            break;
        case NEDBadgeCountPosition_LeftBottom:
            make.right.equalTo(alignmentView.mas_left).offset(offsetX);
            make.top.equalTo(alignmentView.mas_bottom).offset(offsetY);
            break;
        case NEDBadgeCountPosition_LeftCenter:
            make.right.equalTo(alignmentView.mas_left).offset(offsetX);
            make.centerY.equalTo(alignmentView.mas_centerY).offset(offsetY);
            break;
        case NEDBadgeCountPosition_TopCenter:
            make.centerX.equalTo(alignmentView.mas_centerX).offset(offsetX);
            make.centerY.equalTo(alignmentView.mas_top).offset(offsetY);
            break;
            
        default:
            break;
    }
    make.width.mas_equalTo(width);
    make.height.mas_equalTo(height);
    [make install];
    
    [self layoutSubviews];
}

@end
